package com.qiezikaka.kaka.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.view.View;

public class CircleOverlay1 extends View {
    private Paint paint;
    private Path circlePath;

    public CircleOverlay1(Context context) {
        super(context);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.TRANSPARENT); // 完全透明，用于裁剪圆形区域外的内容
        paint.setStyle(Paint.Style.FILL);

        // 创建圆形路径
        circlePath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // 在视图大小改变时更新圆形路径
        float centerX = w / 2f;
        float centerY = h / 2f;
        float radius = Math.min(w, h) / 2f;
        circlePath.reset();
        circlePath.addCircle(centerX, centerY, radius, Path.Direction.CCW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = canvas.getWidth();
        int height = canvas.getHeight();

        // 绘制一个半透明黑色背景
        paint.setColor(Color.parseColor("#88000000")); // 半透明黑色
        canvas.drawRect(0, 0, width, height, paint);

        // 设置Xfermode来实现透明效果
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawPath(circlePath, paint);
        paint.setXfermode(null); // 重置Xfermode
    }
}
